//
//  UIBarButtonItem+WhzCategory.m
//  WhzMT
//
//  Created by whz on 16/12/9.
//  Copyright © 2016年 whz. All rights reserved.
//

#import "UIBarButtonItem+WhzCategory.h"
#import "Header.h"

@implementation UIBarButtonItem (WhzCategory)

+ (instancetype)barButtonItemWithTarget:(id)target action:(SEL)action icon:(NSString *)icon highLightIcon:(NSString *)highLightIcon {

    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn setImage:[UIImage imageNamed:icon] forState:UIControlStateNormal];
    [btn setImage:[UIImage imageNamed:highLightIcon] forState:UIControlStateHighlighted];
    
    // 设置图像大小
    btn.size = CGSizeMake(60, 30);
    
    // 点击监听
    [btn addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    
    return [[UIBarButtonItem alloc] initWithCustomView:btn];
}

+ (instancetype)barButtonItemWithView:(UIView *)view Target:(id)target action:(SEL)action icon:(NSString *)icon highLightIcon:(NSString *)highLightIcon title1:(NSString *)title1 title2:(NSString *)title2 {
    
    UIButton *btnCategory = [UIButton buttonWithType:UIButtonTypeCustom];
    [btnCategory setImage:[UIImage imageNamed:icon] forState:UIControlStateNormal];
    [btnCategory setImage:[UIImage imageNamed:highLightIcon] forState:UIControlStateHighlighted];
    btnCategory.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    [view addSubview:btnCategory];
    [btnCategory mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(view);
    }];
    [btnCategory addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    
    if (title2 == nil) {
        
        UILabel *labelCategory1 = [[UILabel alloc] init];
        labelCategory1.text = title1;
        [view addSubview:labelCategory1];
        [labelCategory1 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.mas_equalTo(UIEdgeInsetsMake(0, 40, 0, 0));
        }];
        
    } else {
    
        UILabel *labelSort1 = [[UILabel alloc] init];
        labelSort1.text = title1;
        [view addSubview:labelSort1];
        [labelSort1 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(view).offset(40);
            make.top.equalTo(view);
            make.height.mas_equalTo(view.bounds.size.height * 0.5);
        }];
        
        UILabel *labelSort2 = [[UILabel alloc] init];
        labelSort2.text = title2;
        [view addSubview:labelSort2];
        [labelSort2 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(view).offset(40);
            make.top.equalTo(labelSort1.mas_bottom).offset(0);
            make.height.mas_equalTo(view.bounds.size.height * 0.5);
        }];
    }
    
    return [[UIBarButtonItem alloc] initWithCustomView:view];
}

@end
